$(function () {
    $("body").on('click','.order-status-box',function () {
        var _this = $(this);
        var id = _this.data('id');
        var status = _this.data('status');
        if(status =='DELAY'){
            delayCourse(id);
        }else{
            var message = '';
            if(status=='REFUND'){
                message = '<div>&nbsp;&nbsp;请确认用户是否学习过此课程，退费将会把购课金额退回至用户前台个人账户中。<p>&nbsp;&nbsp;确认要退费吗？</p></div>';
            }
            if(status=='CLOSE'){
                message ='确认要关闭该流水记录对应的课程记录？';
            }
            icModale(2,message,3,function () {
                updateDetailsStatus(id,status,_this);
            });
        }
    });

    /**
     * 延期课程
     */
    function delayCourse(id) {
        parent.layer.open({
            area:  ['620px','420px'],
            btn: ['确定','关闭'],
            content: baseloaction + "/admin/order/details/ajaxDelayDetails?detailsId=" + id,
            shade: [0.3, '#000'],
            shadeClose: false,
            title: '延期课程',
            type: 2,
            yes: function (index) {
                var validTime = parent.layer.getChildFrame('body', index).find('#validTime').val();
                $.ajax({
                    url:baseloaction + '/admin/order/details/updateCourseBuyRecordValidTime',
                    type:'post',
                    dataType:'json',
                    data:{'detailsId':id,'validTime':validTime},
                    async:false,
                    success:function (result) {
                        icModale(0, result.message);
                    },
                    error:function (error) {
                        icModale(0, "系统错误，延期课程有效期失败");
                    }
                });
                parent.layer.close(index);
            }
        });
    }

    /**
     * 修改流水状态
     * @param id
     * @param status
     * @param em
     */
    function updateDetailsStatus(id,status,em) {
        $.ajax({
            url:baseloaction +'/admin/order/details/updateOrderDetailsStatus',
            type:'post',
            dataType:'json',
            data:{
                'id':id,
                'detailsStatus':status
            },
            success:function (result) {
                if(result.success==true){
                    if(status=='REFUND'){//退费
                        em.parents('tr').remove();
                    }
                    if(status=='CLOSE'){//关闭课程
                        em.parent().remove();//删除关闭按钮
                        $("#details-status-box-"+id).html('<span class="label label-default">已关闭</span>');
                    }
                }else{
                    icModale(0,result.message);
                }
            },
            error:function (error) {
                icModale(0,'系统错误，操作失败');
            }
        });
    }
});